package com.LeeCode;

import java.util.Arrays;

/**
 * 颜色分类
 */

public class Code075 {
    public static void main(String[] args) {
        int[] nums = {0, 1};
        new Code075().sortColors1(nums);
        System.out.println(Arrays.toString(nums));
    }

    public void sortColors(int[] nums) {
        Arrays.sort(nums);
    }

    public void sortColors1(int[] nums) {
        int cur = 0, tmp;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                tmp = nums[i];
                nums[i] = nums[cur];
                nums[cur] = tmp;
                cur++;
            }
        }
        for (int i = cur; i < nums.length; i++) {
            if (nums[i] == 1) {
                tmp = nums[i];
                nums[i] = nums[cur];
                nums[cur] = tmp;
                cur++;
            }
        }
    }

    public void sortColors2(int[] nums) {
        int n = nums.length;
        int p0 = 0, p1 = 0;
        for (int i = 0; i < n; ++i) {
            if (nums[i] == 1) {
                int temp = nums[i];
                nums[i] = nums[p1];
                nums[p1] = temp;
                ++p1;
            } else if (nums[i] == 0) {
                int temp = nums[i];
                nums[i] = nums[p0];
                nums[p0] = temp;
                if (p0 < p1) {
                    temp = nums[i];
                    nums[i] = nums[p1];
                    nums[p1] = temp;
                }
                ++p0;
                ++p1;
            }
        }
    }
}
